package com.itobase.questionnaire.service.impl;

import com.itobase.questionnaire.model.EnumList.MOItem;
import com.itobase.questionnaire.model.Survey;
import com.itobase.questionnaire.repository.SurveyRepository;
import com.itobase.questionnaire.service.IUrlService;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.namespace.QName;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author liucong
 * @date 2018/8/6
 */

@org.springframework.stereotype.Service
public class UrlService implements IUrlService {

    @Autowired
    SurveyRepository surveyRepository;

    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Integer sendMessage(Survey survey) {
        // TODO: 2018/8/6 send message to client


        return null;
    }



    @Override
    public void sendTest(List<String> list, String surveyId){
        try {
            Survey survey = surveyRepository.findOne(surveyId);
            /**
             * 补发短信
             */
            survey.getUserList_informed().addAll(survey.getUserList());
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < list.size(); i++) {
                String phoneNumber = list.get(i);
                if (i == 0) {
                    stringBuffer.append(phoneNumber);
                } else {
                    stringBuffer.append("," + phoneNumber);
                }
            }

            System.out.println(stringBuffer.toString());

            String url = survey.getUrl().substring(7);
            String wsdlUrl = "http://211.138.112.183/smsinterface/services/GaoxtSMSInterfaceForSchools?wsdl ";
            String nameSpaceUri = "http://211.138.112.183/smsinterface/services/GaoxtSMSInterfaceForSchools";
            // 创建调用对象
            Service service = new Service();
            Call call = (Call) service.createCall();
            QName qn = new QName("http://211.138.112.183/smsinterface/services/GaoxtSMSInterfaceForSchools", "moitem");
            call.registerTypeMapping(MOItem.class, qn, new BeanSerializerFactory(MOItem.class, qn), new BeanDeserializerFactory(MOItem.class, qn));
            // 调用getMessage
            call.setOperationName(new QName(nameSpaceUri, "sendSMSByMp"));
            call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
            //int ret = (Integer)call.invoke(new Object[] {});
            String loginName = "zjgsu-fzghc";
            String passWord = "28877526qym";
            String schoolCode = "10353";
            String smsMsg = "您好，您已被浙江工商大学任期目标管理工作领导小组办公室发布的“" + survey.getTitle() + "”测试抽中,请点击 " + url + " 进行答题，账号为您的学号/工号，密码为此手机号";
            String sendTime = format.format(new Date());
            String mpNumber = stringBuffer.toString();
            long userDefId = 2;
            //MOItem[] MOItems = (MOItem[]) call.invoke(new Object[]{loginName,passWord,schoolCode});
            Object ret = (Object) call.invoke(new Object[]{loginName, passWord, schoolCode, smsMsg, sendTime, mpNumber, userDefId});
            String str = ret.toString();
            System.out.println("return value is" + str);
            if (str.equals("0")) {
                surveyRepository.save(survey);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public String send_2(String numbers,String content){
        try {
            String wsdlUrl = "http://211.138.112.183/smsinterface/services/GaoxtSMSInterfaceForSchools?wsdl ";
            String nameSpaceUri = "http://211.138.112.183/smsinterface/services/GaoxtSMSInterfaceForSchools";
            // 创建调用对象
            Service service = new Service();
            Call call = (Call)service.createCall();
            QName qn = new QName("http://211.138.112.183/smsinterface/services/GaoxtSMSInterfaceForSchools","moitem");
            call.registerTypeMapping(MOItem.class, qn, new BeanSerializerFactory(MOItem.class,qn), new BeanDeserializerFactory(MOItem.class,qn));
            // 调用getMessage
            call.setOperationName(new QName(nameSpaceUri, "sendSMSByMp"));
            call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
            //int ret = (Integer)call.invoke(new Object[] {});
            String loginName = "zjgsu-fzghc";
            String passWord = "28877526qym";
            String schoolCode = "10353";
            String smsMsg = content;
            String sendTime = format.format(new Date());
            String mpNumber = numbers;
            long userDefId = 2;
            //MOItem[] MOItems = (MOItem[]) call.invoke(new Object[]{loginName,passWord,schoolCode});
            Object ret = (Object)call.invoke(new Object[] {loginName,passWord,schoolCode,smsMsg,sendTime,mpNumber,userDefId});
            String value = ret.toString();
            System.out.println("return value is"+value);
            return value;
        }catch(Exception ex){
            ex.printStackTrace();
            return "0";
        }
    }
}


